﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OverStore.Runtime;
using OverStore.Runtime.Accessors;
using OverStore.Runtime.Mapping;
using OverStore.Runtime.Storage;
using System.Collections.ObjectModel;
using OverStore.Configuration;
using OverStore.TutorialAsTest.UnitTest.Configuration.Mocks;
using OverStore.Configuration.Components;
using OverStore.Configuration.Mapping;

namespace OverStore.TutorialAsTest.UnitTest.Components.Mapper
{
    [TestClass]
    public class Mapper_Test
    {
        [TestMethod]
        public void CompoundMapper_GetValue()
        {
            var personMapper = CreatePersonMapper();
            var person = new Person()
            {
                Id = 12,
                Gender = Gender.Male,
                Name = "John",
                CreatedBy = DateTime.Now
            };
            IRecord personData = personMapper.GetObjectData(person, new MockPersistenceServiceProvider());
            Assert.AreEqual(person.Id, personData.GetValue("F_ID"));
            Assert.AreEqual((int)person.Gender, (int)personData.GetValue("F_Gender"));
            Assert.AreEqual(person.Name, personData.GetValue("F_Name"));

            Assert.AreEqual(person.CreatedBy, personData.GetValue("F_CreatedBy"));
            //Assert.IsNull(personData.GetValue("F_Mom"));

            //person.Mom = new Person
            //                 {
            //                     Id = 200
            //                 };

            //Assert.AreEqual(person.Mom.Id, personData.GetValue("F_Mom"));

            person.Id = 211;
            person.Name = "John11";

            Assert.AreEqual(person.Id, personData.GetValue("F_ID"));
            Assert.AreEqual(person.Name, personData.GetValue("F_Name"));

        }

        [TestMethod]
        public void CompoundMapper_SetValue()
        {
            var personMapper = CreatePersonMapper();
            var dateTime = DateTime.Now;
            var record = new DictionaryTypedRecord(
                new KeyValuePair<string, object>("F_ID", 2),
                new KeyValuePair<string, object>("F_Name", "John"),
                new KeyValuePair<string, object>("F_CreatedBy", dateTime),
                new KeyValuePair<string, object>("F_Gender", 10));

            var person = new Person();

            personMapper.ApplyObjectData(record, person, new MockPersistenceServiceProvider());

            Assert.AreEqual(2, person.Id);
            Assert.AreEqual(person.Name, "John");
            Assert.AreEqual(person.CreatedBy, dateTime);
            Assert.AreEqual(person.Gender, Gender.Male);
        }
    }
}
